﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DAO;

/// <summary>
/// Manages operations on User story like show detail, edit, create and delete
/// </summary>
public partial class UserStoryDetails : HierarchicalObjectDetails<UserStory,Project>
{
    /// <summary>
    /// Loads the extended inputs from.
    /// </summary>
    protected override IList<ScrumInput> LoadExtendedInputs()
    {
        return this.LoadExtendedInputsFrom(PanelItemDetails);
    }

    /// <summary>
    /// Loads the available superior items(=Sprints).
    /// </summary>
    protected override void LoadAvailableSuperiorItems()
    {
        //if (this.SuperiorItemID == -1)
        //{
            //AvailableSuperiorItems = DAOSprint.GetAllSprintsForProject(this.ProjectID);
        //}
        AvailableSuperiorItems = DAOProject.GetAllProjects();

        //AvailableSuperiorItems = DAOSprint.GetAllSprints();
    }

    /// <summary>
    /// Displays all fields of the selected item into textboxes in form.
    /// </summary>
    public override void DisplayItem(UserStory userStory)
    {
        this.Visible = true;
        
        //clear selections
        this.Type.ClearSelection();
        this.AllProjects.ClearSelection();
        //this.AllSprints.ClearSelection();

        // superior items
        this.AllProjects.DataSource = AvailableSuperiorItems;
        this.AllProjects.DataBind();

        bool exists = userStory != null;

        // values
        this.Header.LastChange =            exists ? userStory.LastChange   : DateTime.MinValue;
        this.Name.Text =                    exists ? userStory.Name         : "";
        this.Description.Text =             exists ? userStory.Description  : "";
        int userStoryType =                 exists ? userStory.Type         : 1;
        this.Priority.SelectedPriority =    exists ? userStory.Priority     : 2;
        this.States.SelectedState =         exists ? userStory.State        : State.DefaultState(State.StateTypes.User_Story);
        this.ProgressBar.Progress =         exists ? userStory.Progress     : 0;
        this.Estimation.Value =             exists ? userStory.Estimation   : 0;
        this.Type.Items.FindByValue(userStoryType.ToString()).Selected = true;
        int superiorItemID =                exists ? userStory.SuperiorItemID : this.SuperiorItemID;
        if (superiorItemID != -1)
        {
            this.AllProjects.SelectedValue = superiorItemID.ToString();
            //this.AllSprints.DataSource = DAOSprint.GetAllSprintsForProject(this.ProjectID);
            //this.AllSprints.DataBind();
            //this.AllSprints.Items.Insert(0,new ListItem((string)GetGlobalResourceObject("Filters","noneUserStory"),"-1"));
        }
        
        // enabled
        this.States.Enabled = exists;
        //this.AllSprints.Enabled = exists;
        

        // visible
        this.ProgressBar.Visible = exists;
        
        // readonly
    }

    /// <summary>
    /// Creates new UserStory
    /// </summary>
    /// <returns>Created UserStory</returns>
    public override UserStory CreateItem()
    {
        int projectID = Int32.Parse(AllProjects.SelectedValue);
        UserStory us = new UserStory(
              0
            , projectID
            , Name.Text
            , Description.Text
            , Estimation.Value
            , States.SelectedState.ID
            , Int32.Parse(this.Type.SelectedValue)
            , ProgressBar.Progress
            , Priority.SelectedPriority
            , DateTime.Now
            , 0 // TODO: vediet nastavit pre userstory sprint?
        );
        UserStory newUS = DAOUserStory.CreateNewUserStory(us, projectID);
        return newUS;
    }

    /// <summary>
    /// Edits UserStory according to data in UserStory detail panel
    /// </summary>
    /// <param name="usOldId">UserStory to update</param>
    public override UserStory EditItem(int usOldId)
    {
        int projectID = Int32.Parse(AllProjects.SelectedValue);
        UserStory us = new UserStory(
              usOldId
            , projectID
            , Name.Text
            , Description.Text
            , Estimation.Value
            , States.SelectedState.ID
            , Int32.Parse(this.Type.SelectedValue)
            , ProgressBar.Progress
            , Priority.SelectedPriority
            , DateTime.Now
            , 0 // TODO: vediet nastavit pre userstory sprint?
        );
        DAOUserStory.UpdateUserStory(us, projectID);
        this.Visible = false;
        this.SuperiorItemID= System.Convert.ToInt32(AllProjects.SelectedValue);
        DisplayItem(us);
        return us;
    }

    /// <summary>
    /// Deletes UserStory
    /// </summary>
    /// <param name="rel">UserStory to delete</param>
    public override void DeleteItem(UserStory userStory)
    {
        DAOUserStory.DeleteUserStory(userStory);
        this.Visible = false;
    }

    /// <summary>
    /// Validates the input.
    /// </summary>
    /// <returns></returns>
    public override bool ValidateInput()
    {
        throw new NotImplementedException();
        return true;
    }

    protected override Panel panelWithControls { get { return this.PanelItemDetails; } }
}
